What is request-promise-native?
The request-promise-native npm package is a simplified HTTP request client for Node.js, which supports Promises. It is built on top of the popular 'request' library and provides a more modern and convenient way to handle HTTP requests using native ES6 promises.
What are request-promise-native's main functionalities?
Making a GET request
This feature allows you to make a simple GET request to a specified URL and handle the response using promises.
const rp = require('request-promise-native');
rp('https://jsonplaceholder.typicode.com/posts/1')
.then(function (response) {
console.log(JSON.parse(response));
})
.catch(function (err) {
console.error(err);
});
Making a POST request
This feature allows you to make a POST request with a JSON body to a specified URL and handle the response using promises.
const rp = require('request-promise-native');
const options = {
method: 'POST',
uri: 'https://jsonplaceholder.typicode.com/posts',
body: {
title: 'foo',
body: 'bar',
userId: 1
},
json: true
};
rp(options)
.then(function (response) {
console.log(response);
})
.catch(function (err) {
console.error(err);
});
Handling request options
This feature allows you to customize your HTTP request by specifying various options such as headers, query parameters, and response format.
const rp = require('request-promise-native');
const options = {
uri: 'https://jsonplaceholder.typicode.com/posts/1',
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
rp(options)
.then(function (response) {
console.log(response);
})
.catch(function (err) {
console.error(err);
});
Other packages similar to request-promise-native
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a simple and easy-to-use API for making HTTP requests and supports features like interceptors, automatic JSON transformation, and request cancellation. Compared to request-promise-native, Axios has a more modern API and is actively maintained.
node-fetch
Node-fetch is a lightweight module that brings the Fetch API to Node.js. It is a minimalistic library that provides a simple way to make HTTP requests using promises. Compared to request-promise-native, node-fetch has a smaller footprint and is more aligned with the Fetch API standard used in browsers.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It supports promises, streams, retries, and many other advanced features. Compared to request-promise-native, Got offers a more feature-rich and flexible API, making it suitable for more complex use cases.
Request-Promise-Native
Deprecated!
As of Feb 11th 2020, request
is fully deprecated. No new changes are expected to land. In fact, none have landed for some time. This package is also deprecated because it depends on request
.
Fyi, here is the reasoning of request
's deprecation and a list of alternative libraries.
This package is similar to request-promise
but uses native ES6+ promises.
Please refer to the request-promise
documentation. Everything applies to request-promise-native
except the following:
- Instead of using Bluebird promises this library uses native ES6+ promises.
- Native ES6+ promises may have fewer features than Bluebird promises do. In particular, the
.finally(...)
method was not included until Node v10.
Installation
This module is installed via npm:
npm install --save request
npm install --save request-promise-native
request
is defined as a peer-dependency and thus has to be installed separately.
Migration from request-promise
to request-promise-native
- Go through the migration instructions to upgrade to
request-promise
v4. - Ensure that you don't use Bluebird-specific features on the promise returned by your request calls. In particular, you can't use
.finally(...)
anymore. - You are done.
Contributing
To set up your development environment:
- clone the repo to your desktop,
- in the shell
cd
to the main folder, - hit
npm install
, - hit
npm install gulp -g
if you haven't installed gulp globally yet, and - run
gulp dev
. (Or run node ./node_modules/.bin/gulp dev
if you don't want to install gulp globally.)
gulp dev
watches all source files and if you save some changes it will lint the code and execute all tests. The test coverage report can be viewed from ./coverage/lcov-report/index.html
.
If you want to debug a test you should use gulp test-without-coverage
to run all tests without obscuring the code by the test coverage instrumentation.
Change History
- v1.0.9 (2020-07-21)
- Security fix: bumped
request-promise-core
which bumps lodash
to ^4.17.19
following this advisory.
- v1.0.8 (2019-11-03)
- Security fix: bumped
request-promise-core
which bumps lodash
to ^4.17.15
. See vulnerabilty reports.
(Thanks to @aw-davidson for reporting this in issue #49.)
- v1.0.7 (2019-02-14)
- Corrected mistakenly set
tough-cookie
version, now ^2.3.3
(Thanks to @evocateur for pointing this out.) - If you installed
request-promise-native@1.0.6
please make sure after the upgrade that request
and request-promise-native
use the same physical copy of tough-cookie
.
- v1.0.6 (2019-02-14)
- Using stricter
tough-cookie@~2.3.3
to avoid installing tough-cookie@3
which introduces breaking changes
(Thanks to @jasonmit for pull request #33) - Security fix: bumped
lodash
to ^4.17.11
, see vulnerabilty reports
- v1.0.5 (2017-09-22)
- Upgraded
tough-cookie
to a version without regex DoS vulnerability
(Thanks to @sophieklm for pull request #13)
- v1.0.4 (2017-05-07)
- v1.0.3 (2016-08-08)
- Renamed internally used package
@request/promise-core
to request-promise-core
because there where too many issues with the scoped package name
- v1.0.2 (2016-07-18)
- Fix for using with module bundlers like Webpack and Browserify
- v1.0.1 (2016-07-17)
- Fixed
@request/promise-core
version for safer versioning
- v1.0.0 (2016-07-15)
License (ISC)
In case you never heard about the ISC license it is functionally equivalent to the MIT license.
See the LICENSE file for details.